package com.mihuo.entity.common;

import lombok.Data;

/**
 * 通用响应结果封装类
 * @param <T> 响应数据类型
 */
@Data
public class R<T> {
    private final Integer code;
    private final String msg;
    private final T data;

    public static final Integer CODE_SUCCESS = 200;//请求成功
    public static final Integer CODE_ERROR = 500;//请求失败，业务失败
    public static final Integer CODE_ERROR_AUTH = 505;//请求失败，未授权
    public static final Integer CODE_ERROR_ARGUMENT_NOT_VALID = 506;//处理参数校验失败异常（@Validated 触发）
    public static final String MSG_OK = "请求成功";
    public static final String MSG_ERROR_AUTH = "请求未授权";

    private R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 成功响应方法
    public static <T> R<T> success() {

        return new R<>(CODE_SUCCESS, MSG_OK, null);
    }

    public static <T> R<T> success(T data) {

        return new R<>(CODE_SUCCESS, MSG_OK, data);
    }

    public static <T> R<T> success(String msg, T data) {

        return new R<>(CODE_SUCCESS, msg, data);
    }

    public static <T> R<T> error(String msg) {
        return new R<>(CODE_ERROR, msg, null);
    }

    public static <T> R<T> error(Integer code,String msg) {
        return new R<>(code, msg, null);
    }

    public static <T> R<T> error(String msg, T data) {
        return new R<>(CODE_ERROR, msg, data);
    }

    public static <T> R<T> errorAuth(){
        return new R<>(CODE_ERROR_AUTH,MSG_ERROR_AUTH,null);
    }
}